<?php

class LGToolbar {

	private $_item;
	private $_items;

	public function __construct() {
		$this->_items = array();
	}

	public function getItem() {
		return $this->_item;
	}

	public function setItem($value) {
		$this->_item = $value;
	}

	public function custom($id, $title, $text, $action, $event = '', $class = '') {
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" onclick="%s" href="%s">%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $event, $action, $text);
	}

	public function ArticleSave($id, $text) {
		$str = '<button class="button" type="button" id="%s">%s</button>';
		$this->_items[] = sprintf($str, $id, $text);
	}

	public function Button($id, $text) {
		$str = '<button class="button" type="button" id="%s">%s</button>';
		$this->_items[] = sprintf($str, $id, $text);
	}

	public function Cancel($id, $text, $href = "") {
		if($href === "")
		$href = url(app()->controller->id);
		$str = '<button id="%s" type="button" class="button%s" onclick="window.location.href=\'%s\'">%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $href, $text);
	}

	/**
	 * Add cancle button, it's used in Aricle Update
	 * @param type $htmlID
	 * @param type $text
	 * @param type $articleId
	 */
	public function CancelButtonInArticleEditing($htmlID, $text, $articleId) {
		$href = url(app()->controller->id. '/CancelEdit/'.$articleId);

		$href = url(app()->controller->id. '/CancelEdit/'.$articleId);
		$onClick =  sprintf('window.location.href=\'%s\'', $href);

		$this->_items[] = CHtml::button($text, array (
	    'class' => '',
	    'onclick' => $onClick,
	    'id' => $htmlID,
		));
	}

	public function addNew($dialogWidth = '500', $dialogHeight = '400', $title = 'Thêm mới', $text = 'Thêm mới', $event = '', $class = '') {
		$href = url(app()->controller->id . '/create');
		$str = '<button type="button" dialog-width="%s" dialog-height="%s" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" onclick="%s" href="%s">%s</button>';
		$this->_items[] = sprintf($str, $dialogWidth, $dialogHeight, !empty($class) ? ' ' . $class : '', $title, $event, $href, $text);
	}

	public function addNewOnNewPage($id = "linkAddNew", $text = 'Thêm mới', $class = '') {
		$href = url(app()->controller->id . '/create');
		$str = '<button id="%s" type="button" class="button%s" onclick="window.location.href=\'%s\'">%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $href, $text);
	}

	public function addNewHasFile($dialogWidth = '500', $dialogHeight = '400', $title = 'Thêm mới', $text = 'Thêm mới', $event = '', $class = '') {
		$href = url(app()->controller->id . '/create');
		$str = '<button type="button" dialog-width="%s" dialog-height="%s" class="button update-file-dialog-open-link%s"  data-update-dialog-title="%s" onclick="%s" href="%s">%s</button>';
		$this->_items[] = sprintf($str, $dialogWidth, $dialogHeight, !empty($class) ? ' ' . $class : '', $title, $event, $href, $text);
	}

	public function error($title = 'Error', $text = 'Error', $event = '', $class = '') {
		$href = url(app()->controller->id . '/error');
		$str = '<button type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" onclick="%s" href="%s">%s</button>';
		$this->_items[] = sprintf($str, !empty($class) ? ' ' . $class : '', $title, $event, $href, $text);
	}

	public function trashList($id = 'linkTrash', $title = 'Lotus Ocean', $text = 'Trash', $event = "", $class = '') {
		$href = url(app()->controller->id . '/trash');
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" action="%s" onclick="%s" live>%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $href, $event, $text);
	}

	public function checkinList($id = 'linkCheckIn', $title = 'Lotus Ocean', $text = 'Check In', $event = "", $class = '') {
		$href = url(app()->controller->id . '/checkin');
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" action="%s" onclick="%s" live>%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $href, $event, $text);
	}

	public function publishList($id = 'linkPublish', $title = 'Lotus Ocean', $text = 'Publish', $event = "", $class = '') {
		$href = url(app()->controller->id . '/publish');
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" action="%s" onclick="%s" live>%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $href, $event, $text);
	}

	public function unpublishList($id = 'linkUnpublish', $title = 'Lotus Ocean', $text = 'Unpublish', $event = "", $class = '') {
		$href = url(app()->controller->id . '/unpublish');
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" action="%s" onclick="%s" live>%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $href, $event, $text);
	}

	public function refresh($id = 'linkRefresh', $text = 'Làm mới trang', $class = '') {
		$href = url(app()->controller->id . '/index');
		$str = '<button id="%s" type="button" class="button%s" onclick="window.location.href=\'%s\'">%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $href, $text);
	}

	public function mergeToolbar() {
		$result = '';
		foreach ($this->_items as $toolbar) {
			$result .= $toolbar;
		}
		return $result;
	}
	public function ButtonSave($text, $formId, $saveType) {
		$str = '<button id="btnSave" type="button" class="button" onclick="LGCMS.setTask(\'%s\', \'%s\')">%s</button>';
		$this->_items[] = sprintf($str, $formId, $saveType, $text);
	}
	const idPrefix='lg';
	public static $count=0;
	public function button1($text, $title, $class, $extraSettings=array())
	{
		$arrayDefault = array(
                        'id' => self::idPrefix.self::$count++,
                        'title' => $title,
                        'class' => $class);
		if(count($extraSettings) == 0)
		return CHtml::htmlButton($text, $arrayDefault);
		else
		{
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::htmlButton($text, $result);
		}
	}

	public function buttonRefresh($text = 'Làm mới trang', $title = 'Làm mới trang', $url = null){
		if($url == NULL)
		$url = url(app()->controller->id . '/index');

		$this->_items[] = $this->button1($text, $title, 'button', array('onclick'=>'window.location.href=\''. $url .'\''));
	}

	public function buttonConfirm($text, $title = 'Lotus Ocean', $route = null, $params = array(), $event = null, $htmlOptions = null)
	{
		$onclick = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$onclick = sprintf($onclick, self::idPrefix.self::$count, self::idPrefix.self::$count);
		if($event != null)
		$onclick = $event;

		if ($htmlOptions == null) {
			$htmlOptions = array();
		}

		$newHtmlOption = array(
                        'data-update-dialog-title' =>'Lotus Ocean',
                        'action' => url($route,$params),
                        'onclick'=> $onclick,
                        'live'=> '');

		$htmlOptions = array_merge($htmlOptions, $newHtmlOption);
		$this->_items[] = $this->button1($text, $title, 'button update-dialog-open-link',
		$htmlOptions
		);
	}
	/*Minh Mai : Add this button to delete user from client*/
	public function buttonDelWithParam($text, $title = 'Lotus Ocean', $route = null, $params = array(), $event = null)
	{
		$onclick = "$('#%s').attr('href', $('#%s').attr('action') + '&id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$onclick = sprintf($onclick, self::idPrefix.self::$count, self::idPrefix.self::$count);
		if($event != null)
		$onclick = $event;
		$this->_items[] = $this->button1($text, $title, 'button update-dialog-open-link',
		array(
                        'data-update-dialog-title' =>'Lotus Ocean',
                        'action' => url($route,$params),
                        'onclick'=> $onclick,
                        'live'=> ''));
	}

	public function buttonDelete($text = 'Xóa', $title = 'Xóa', $route = null, $params = array(), $htmlOptions = array()) {
		if($route == null)
		$route = app()->controller->id . '/delete';
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		if($params != null)
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '&id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, self::idPrefix.self::$count, self::idPrefix.self::$count);

		$this->_items[] = $this->buttonConfirm($text, $title, $route, $params,$event, $htmlOptions);
	}

	public function deleteList($id = 'linkDelete', $title = 'Lotus Ocean', $text = 'Delete', $event = "", $class = '') {
		$href = url(app()->controller->id . '/delete');
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
		$str = '<button id="%s" type="button" class="button update-dialog-open-link%s"  data-update-dialog-title="%s" action="%s" onclick="%s" live>%s</button>';
		$this->_items[] = sprintf($str, $id, !empty($class) ? ' ' . $class : '', $title, $href, $event, $text);
	}
	public function buttonAddNew($dialogWidth = '500', $dialogHeight = '400', $dialogTitle = 'Thêm mới', $text = 'Thêm mới', $title = 'Thêm mới', $isHasFile = False, $route = null, $params = array())
	{
		if($route == NULL)
		$route = app()->controller->id . '/create';
		$class = "button update-dialog-open-link";
		if($isHasFile == TRUE)
		$class = "button update-file-dialog-open-link";
		$this->_items[] = $this->button1($text, $title, 'button',
		array(
                        'dialog-width'=> $dialogWidth,
                        'dialog-height' => $dialogHeight,
                        'class' => $class,
                        'href'=>url($route, $params),
                        'data-update-dialog-title'=>$dialogTitle));
	}

	public function buttonCancel($id = 'buttonCancel', $text = 'Hủy', $title = 'Hủy', $url = null)
	{
		$href = url(app()->controller->id);
		if($url != NULL)
		$href = $url;
		$this->_items[] = $this->button1($text, $title, 'button', array('onclick'=>'window.location.href=\''. $href .'\'', 'id'=> $id));
	}

	public function buttonNewPage($text = 'New Page', $class = '',$route = null, $params = array()) {
		$this->_items[] = $this->button1($text, '', 'button'.$class,
		array("onclick"=>"window.location.href='" . url($route, $params) . "'"));
	}

	public function buttonModal($dialogWidth = '500', $dialogHeight = '400', $dialogTitle = 'Thêm mới', $text = 'Thêm mới', $route = null, $params = array(), $class = '') {
		$this->_items[] = $this->button1($text,'', 'button',
		array(
                        'dialog-width'=> $dialogWidth,
                        'dialog-height' => $dialogHeight,
                        'class' => "button update-dialog-open-link ".$class,
                        'href'=>url($route, $params),
                        'data-update-dialog-title'=>$dialogTitle));
	}
        
    public function buttonExport($text = 'Export',$title='Export', $id= null, $class = '',$route = null, $params = array()) {
		if($id==null)
			$id = self::idPrefix.self::$count++;
    	$arrayDefault = array(
                        'id' => $id,
                        'title' => $title,
                        'class' => 'button'.$class,
    					'action' => url($route, $params));
		$this->_items[] =  CHtml::htmlButton($text, $arrayDefault);
	}
	
	public function  buttonDetail($text = 'Detail',$title='Detail', $id= null, $class = '',$route = null, $params = array()) {
		if($id==null)
			$id = self::idPrefix.self::$count++;
    	$arrayDefault = array(
                        'id' => $id,
                        'title' => $title,
                        'class' => 'button'.$class,
    					'onclick' => "window.location.href='" . url($route, $params) . "'");
		$this->_items[] =  CHtml::htmlButton($text, $arrayDefault);
	}
	
	public function buttonCustom($text, $title, $class, $extraSettings=array())
	{
		$this->_items[] = $this->button1($text, $title, $class, $extraSettings);
	}
}